<template>
	<view class="setUp">
		<view class="setUp_item" @click="getAboutUs">
			<view class="name">关于我们</view>
			<view class="setUp_item_image">
				<image src="../../../static/image/mine/fanhui.png"></image></view>
		</view>
		<view class="setUp_item" @tap="clearCache()">
			<view class="name">清理缓存</view>
			<view class="title">
				{{fileSizeString}}
				</view>
		</view>
	</view>
</template>

<script>
	import {mapState} from 'vuex';
	export default {
		data() {
			return {
				fileSizeString: '0 B',
			};
		},
		computed: {
			...mapState(['user','common'])
		},
		onLoad() {
			// #ifdef APP-PLUS
			this.formatSize();
			// #endif
		},
		methods:{
			formatSize() {
			    plus.cache.calculate((size)=> {  
			        let sizeCache = parseInt(size);  
			        if (sizeCache == 0) {  
			            this.fileSizeString = "0 B";  
			        } else if (sizeCache < 1024) {  
			            this.fileSizeString = sizeCache + " B";  
			        } else if (sizeCache < 1048576) {  
			            this.fileSizeString = (sizeCache / 1024).toFixed(2) + " KB";  
			        } else if (sizeCache < 1073741824) {  
			            this.fileSizeString = (sizeCache / 1048576).toFixed(2) + " MB";  
			        } else {  
			            this.fileSizeString = (sizeCache / 1073741824).toFixed(2) + " GB";  
			        }  
			    }); 
			},
			clearCache() {
				// #ifndef APP-PLUS
				this.$wanlshop.msg('没有发现任何缓存');
				// #endif
				// #ifdef APP-PLUS
				uni.showModal({
				    title: '提示',
				    content: '确定要清除缓存',
				    success: (res)=> {
				        if (res.confirm) {
				           if (plus.os.name == 'Android') {
				               let main = plus.android.runtimeMainActivity();  
				               let sdRoot = main.getCacheDir();  
				               let files = plus.android.invoke(sdRoot, "listFiles");  
				               let len = files.length;  
				               for (let i = 0; i < len; i++) {  
				                   let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径  
				                   plus.io.resolveLocalFileSystemURL(filePath, (entry)=> {  
				                       if (entry.isDirectory) {  
				                           entry.removeRecursively((entry)=> { //递归删除其下的所有文件及子目录  
				           					this.$wanlshop.msg('缓存清理完成');
				                               this.formatSize(); // 重新计算缓存  
				                           }, (e)=> {  
				                               console.log(e.message)  
				                           });  
				                       } else {  
				                           entry.remove();  
				                       }  
				                   }, (e)=> {  
				                       console.log('文件路径读取失败')  
				                   });  
				               }  
				           } else { // ios暂时未找到清理缓存的方法，以下是官方提供的方法，但是无效，会报错  
				               plus.cache.clear(()=> {  
				                   this.$wanlshop.msg('缓存清理完成');
				                   this.formatSize();  
				               });  
				           }  
				        }
				    }
				});
			    
				// #endif
			},
			// 点击关于我们跳转的关于我们页面
			getAboutUs(){
				uni.navigateTo({
					url:'../AboutUs/AboutUs'
				})
			}
		}
	}
</script>

<style lang="scss">
page {
		background-color: #f0f0f0;
	}
.setUp{
	.setUp_item{
		background-color: #FFFFFF;
        height: 100rpx;
		
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin: 4rpx 0;
		padding: 0 30rpx;
		.name{
			font-size: 30rpx;
			color:#000000 ;
		}
		&_image{
			// background-color: pink;
			width: 18rpx;
			height: 28rpx;
			display: flex;
			align-items: center;
			image{
				width: 18rpx;
				height: 28rpx;
			}
		}
		.title{
			font-size: 26rpx;
			color: #333333;
			background-color: #F1F1F1;
			border-radius: 50rpx;
			padding: 10rpx 20rpx;
			text-align: center;
		}
	}
}
</style>
